package com.example.mybiadushoujiweishi.activity.acivitys.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;


import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.Contact;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;
import com.thinkcool.circletextimageview.CircleTextImageView;


import java.util.List;

/**
 * Created by Administrator on 2017/6/6 0006.
 */

public class AddContactsAdapter extends RecyclerView.Adapter<AddContactsAdapter.Myviewholder> {
    List<Contact> list;
    Context context;
    static List<Contact> listsos = MyApplication.getSoslist();

    public static List<Contact> getListsos() {
        return listsos;
    }
    TextView textViewone;
    // int count;

    public AddContactsAdapter(List<Contact> list,TextView textView) {

        this.list = list;
        this.textViewone=textView;
    }

    @Override
    public Myviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_contacts_iteml, parent, false);
        Myviewholder myviewholder = new Myviewholder(view);
        return myviewholder;
    }

    @Override
    public void onBindViewHolder(Myviewholder holder, int position) {
        Contact contact = list.get(position);
        holder.circleTextImageView.setText(contact.getData());
        holder.circleTextImageView.setFillColor(contact.getColors());
        holder.circleTextImageView.setTextColor(Color.WHITE);
        holder.Contact_name_text.setText(contact.getData());
        holder.Contact_telnumber_text.setText(contact.getPhonenum());
        holder.Contact_addr_text.setText(contact.getAddress());


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Myviewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView Contact_name_text;
        TextView Contact_telnumber_text;
        TextView Contact_addr_text;
        CircleTextImageView circleTextImageView;
        CheckBox checkbox;

        public Myviewholder(View itemView) {
            super(itemView);
            Contact_name_text = (TextView) itemView.findViewById(R.id.contact_name);
            Contact_telnumber_text = (TextView) itemView.findViewById(R.id.contact_telnumber);
            Contact_addr_text = (TextView) itemView.findViewById(R.id.contact_addr);
            circleTextImageView = (CircleTextImageView) itemView.findViewById(R.id.photo);
            checkbox = (CheckBox) itemView.findViewById(R.id.contact_checkbox);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Contact contact = list.get(getAdapterPosition());
            Toast.makeText(MyApplication.getContext(), contact.getData(), Toast.LENGTH_SHORT).show();
            String phonenumber = contact.getPhonenum();
            String tel = phonenumber.replace(" ", "").replace("-", "");
            if (tel.length() == 11 && tel.matches("^1[3578]\\d{9}$")) {
                if (checkbox.isChecked()) {
                    checkbox.setChecked(false);
                    listsos.remove(contact);
                   textViewone.setText("("+MyApplication.getSoslist().size()+")");
                } else {

                    if (listsos.size() < 3) {
                        checkbox.setChecked(true);

                        if (!listsos.contains(contact)) {
                            listsos.add(contact);
                            textViewone.setText("("+MyApplication.getSoslist().size()+")");
                        }

                    } else {
                        Toast.makeText(MyApplication.getContext(), "不能超过三个", Toast.LENGTH_SHORT).show();
                    }


                }
            }

        }

    }

}
